//
//  TZWebController.m
//  BSBDJ
//
//  Created by user on 16/5/25.
//  Copyright © 2016年 Burning. All rights reserved.
//

#import "TZWebController.h"
#import <WebKit/WebKit.h>
@interface TZWebController ()<WKUIDelegate>

@property (nonatomic, weak) WKWebView *webView;
/** progressView*/
@property (nonatomic ,weak) UIProgressView *progressView;

@property (nonatomic, weak) UIView *toolView;

@end

@implementation TZWebController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUI];
    
    NSLog(@"%@",self.url);
}
// 设置UI
- (void)setUI{
    self.view.backgroundColor = XMGColor(244, 244, 244);
    [self setupWebView];
    [self setupProgressView];
    [self setupToolBar];
}
// 设置webView
- (void)setupWebView{
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 1, TZScreenW, TZScreenH)];
    webView.UIDelegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
    [webView loadRequest:request];
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    _webView = webView;
    [self.view addSubview:webView];
}
// 设置progressViwe
- (void)setupProgressView{
    UIProgressView *progressV = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 64, TZScreenW, 1)];
    _progressView = progressV;
    [self.view addSubview:progressV];
}


// 设置选择跳
- (void)setupToolBar{
    UIView *toolbar = [[UIView alloc] initWithFrame:CGRectMake(0, TZScreenH-40, TZScreenW, 40)];
    toolbar.backgroundColor = XMGColor(244, 244, 244);
    _toolView = toolbar;
    [[UIApplication sharedApplication].keyWindow addSubview:toolbar];

    UIButton *goBtn = [UIButton new];
    [goBtn setTitle:@"前进" forState:0];
    [goBtn setTitleColor:XMGColor(64, 164, 234) forState:0];
    [goBtn addTarget:self action:@selector(goBtnClick) forControlEvents:UIControlEventTouchUpInside];
    goBtn.frame = CGRectMake(20, 0, 40, toolbar.height);
    [toolbar addSubview:goBtn];
    
    UIButton *backBtn = [UIButton new];
    [backBtn setTitle:@"后退" forState:0];
    [backBtn setTitleColor:XMGColor(64, 164, 234) forState:0];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    backBtn.frame = CGRectMake(CGRectGetMaxX(goBtn.frame) + 20, 0, 40, toolbar.height);
    [toolbar addSubview:backBtn];
    
    UIButton *reloadBtn = [UIButton new];
    [reloadBtn setTitle:@"刷新" forState:0];
    [reloadBtn setTitleColor:XMGColor(64, 164, 234) forState:0];
    [reloadBtn addTarget:self action:@selector(reloadBtnClick) forControlEvents:UIControlEventTouchUpInside];
    reloadBtn.frame = CGRectMake(TZScreenW - 60, 0, 40, toolbar.height);
    [toolbar addSubview:reloadBtn];
}

- (void)goBtnClick{
    if (self.webView.canGoForward){
        [self.webView goForward];
    }
}

- (void)backBtnClick{
    if (self.webView.canGoBack) {
        [self.webView goBack];
    }
}

- (void)reloadBtnClick{
    [self.webView reload];
}





- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    float new = [change[NSKeyValueChangeNewKey] floatValue];
    NSLog(@"%f",new);
    self.progressView.progress = new;
    self.progressView.hidden = _progressView.progress>=1;
}
/**  移除KVO
 'An instance 0x7a492c00 of class WKWebView was deallocated while key value observers were still registered with i*/

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.toolView removeFromSuperview];

}



- (void)dealloc
{
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.toolView removeFromSuperview];
    NSLog(@"delloc----me");

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
